package com.fengxing.ams.web.facade.mouble.user;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

public interface IUserDAO extends PagingAndSortingRepository<UserVO, Long>, JpaSpecificationExecutor<UserVO> {

	@Query("select count(1) from UserVO u where u.email = :email")
	int countByEmail(@Param("email") String email);
	
	UserVO findByEmail(String email);
	
	@Query(value = "select * from zyt_sys_user where email in (:email)", nativeQuery=true)
	List<UserVO> findListByEmail(@Param("email")List<String> email);
	
	Page<UserVO> findAllByUserIdIn(Collection<Long> userIds, Pageable pageable);
	
	@Modifying
	@Query("update UserVO set name= :name, photo = :photo, mobile= :mobile where userId = :id")
	void updateUserDetail(@Param("id")Long id, @Param("name")String name, @Param("photo")String photo, @Param("mobile")String mobile);

	@Modifying
	@Query("update UserVO set password =:newPassword where userId = :id")
	void updatePasswordById(@Param("id")Long id, @Param("newPassword")String newPassword);
	
	@Modifying
	@Query("update UserVO set lastLoginIp =:lastLoginIp, lastLoginDate =:lastLoginDate where userId = :id")
	void updateLoginInfo(@Param("id")Long id, @Param("lastLoginIp")String lastLoginIp, @Param("lastLoginDate")Date lastLoginDate);
	
	@Modifying
	@Query("update UserVO set name =:name, gendar =:gendar, score=:score, update_date=:update_date, subject=:subject where userId = :id")
	void updateLoginInfoProfile(@Param("id")Long id, @Param("name")String name, @Param("gendar")String gendar, @Param("score")Integer score, @Param("subject")String subject,  @Param("update_date")Date updateDate);

	UserVO findByMobile(String mobile);

	@Modifying
	@Query("update UserVO set mobile =:mobile where userId = :id")
	void updateMobile(@Param("mobile")String mobile, @Param("id")Long userId);

	@Modifying
	@Query(value="update UserVO set password =:password where email = :email", nativeQuery=false)
	void resetPwd(@Param("email")String email, @Param("password")String pwd);

	@Query(value="select email from UserVO")
	List<String> findAllStudentId();

	@Modifying
	@Query(value="delete from zyt_sys_user where email in (:email)", nativeQuery=true)
	void deleteByEmails(@Param("email")List<String> ids);

	@Query(value="select * from zyt_sys_user where college_id != 0 and college_id is not NULL", nativeQuery=true)
	List<UserVO> findAllManagerUser();

	@Query(value="select * from zyt_sys_user where college_id = 0", nativeQuery=true)
	UserVO findSupperManagerUser();
}
